package com.ymatou.iapi.productprice.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.itempriceq.service.GetPriceByProdIdCall;
import com.ymatou.iapi.listpriceq.service.GetPriceByCatalogIdsCall;
import com.ymatou.iapi.listpriceq.service.GetPriceByCatalogIdsTradeIsolationCall;
import com.ymatou.iapi.listpriceq.service.GetPriceByProdIdsCall;
import com.ymatou.iapi.listpriceq.service.GetPriceByProdIdsTradeIsolationCall;
import com.ymatou.iapi.productprice.parameter.CatalogPriceBean;
import com.ymatou.iapi.productprice.parameter.ProductPriceBean;
import com.ymttest.utils.FileUtil;

public class ComparePrice2 {

	public static String errorFile = "E:\\fyf\\Testing\\source\\YmatouTest\\Results\\productprice.iapi.ymatou.com\\ComparePrice\\error.txt";
	public static String diffFile = "E:\\fyf\\Testing\\source\\YmatouTest\\Results\\productprice.iapi.ymatou.com\\ComparePrice\\diff.txt";
	public static String retFile = "E:\\fyf\\Testing\\source\\YmatouTest\\Results\\productprice.iapi.ymatou.com\\ComparePrice\\ret.txt";

	// 老价格 服务接口
	public  static PriceInterface old_ByCatalogIds = new GetPriceByCatalogIdsCall();

	public  static PriceInterface old_ByCatalogIdsTradeIsolation = new GetPriceByCatalogIdsTradeIsolationCall();

	public  static PriceInterface old_ByProdIds = new GetPriceByProdIdsCall();

	public  static PriceInterface old_ByProdIdsTradeIsolation = new GetPriceByProdIdsTradeIsolationCall();

	public  static PriceInterface old_ByProdId = new GetPriceByProdIdCall();

	public  static PriceInterface old_ByProdIdTradeIsolation = new GetPriceByProdIdTradeIsolationCall();

	// 新价格服务 接口
	public  static PriceInterface new_ByCatalogIds = new com.ymatou.iapi.productprice.service.GetPriceByCatalogIdsCall();

	public  static PriceInterface new_ByCatalogIdsTradeIsolation = new com.ymatou.iapi.productprice.service.GetPriceByCatalogIdsTradeIsolationCall();

	public  static PriceInterface new_ByProdIds = new com.ymatou.iapi.productprice.service.GetPriceByProdIdsCall();

	public  static PriceInterface new_ByProdIdsTradeIsolation = new com.ymatou.iapi.productprice.service.GetPriceByProdIdsTradeIsolationCall();

	public  static PriceInterface new_ByProdId = new com.ymatou.iapi.productprice.service.GetPriceByProdIdCall();

	public  static PriceInterface new_ByProdIdTradeIsolation = new com.ymatou.iapi.productprice.service.GetPriceByProdIdTradeIsolationCall();

	public static void compare(PriceInterface oldInterface, PriceInterface newInterface, int buyerId,String productIdOrCatalogId, String funticonname) throws JSONException {

		System.out.println("[" + funticonname + "]");

		JSONObject oldRet = oldInterface.GetReturnDataByBuyerAndId(buyerId, productIdOrCatalogId);
		JSONObject newRet = newInterface.GetReturnDataByBuyerAndId(buyerId, productIdOrCatalogId);
		
		try {
			

			ProductPriceBean oldPrice = getPriceBeanByRet(oldRet);
			ProductPriceBean newPrice = getPriceBeanByRet(newRet);
			String productId = oldPrice.getProductId();
			oldPrice.getCatalogPriceBeans().forEach(ele -> {
				CatalogPriceBean newCatalogPriceBean = newPrice.getCatalogPriceBeans().stream()
						.filter(newEle -> newEle.getCatalogId().equals(ele.getCatalogId())).findAny().orElse(null);

				if (newCatalogPriceBean == null) {
					System.out.println("规格id:" + ele.getCatalogId() + ",在新接口返回报文中不存在");
				} else {
					if (ele.getPrice() == newCatalogPriceBean.getPrice()) {
						System.out.println("[" + funticonname + "]价格一致,  ProductId:" + productId + ",  CatalogId:"
								+ ele.getCatalogId() + ", 价格比对  老接口价格:" + ele.getPrice() + ",新接口价格:"
								+ newCatalogPriceBean.getPrice());
						FileUtil.writeAsLine(retFile,
								"[" + funticonname + "] 价格一致,  ProductId:" + productId + ",  CatalogId:"
										+ ele.getCatalogId() + ", 价格比对  老接口价格:" + ele.getPrice() + ",新接口价格:"
										+ newCatalogPriceBean.getPrice());

					} else {
						System.out.println("####[" + funticonname + "] 价格不一致, ProductId:" + productId + ",CatalogId:"
								+ ele.getCatalogId() + ", 价格比对  老接口价格:" + ele.getPrice() + ",新接口价格:"
								+ newCatalogPriceBean.getPrice());

						FileUtil.writeAsLine(diffFile,
								"####[" + funticonname + "] 价格不一致, ProductId:" + productId + ",CatalogId:"
										+ ele.getCatalogId() + ", 价格比对  老接口价格:" + ele.getPrice() + ",新接口价格:"
										+ newCatalogPriceBean.getPrice());
					}
				}

			});
		} catch (Exception e) {
			FileUtil.writeAsLine(errorFile, productIdOrCatalogId + ", " + funticonname +",Old_Msg"+oldRet.getString("Msg") + ",New_Msg" + newRet.getString("Msg"));
			e.printStackTrace();
		}

	}

	public static ProductPriceBean getPriceBeanByRet(JSONObject ret) throws Exception {
		JSONObject dataObj = ret.getJSONObject("Data");
		ProductPriceBean productPriceBean = new ProductPriceBean();

		if (dataObj.has("PriceInfo")) {
			// prodId
			JSONObject retBean = dataObj.getJSONObject("PriceInfo");
			String productId = retBean.getString("ProductId");
			productPriceBean.setProductId(productId);

			JSONArray catalogsJsonArray = retBean.getJSONArray("Catalogs");
			productPriceBean.setProductId(retBean.getString("ProductId"));

			List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
			for (int i = 0; i < catalogsJsonArray.length(); i++) {
				JSONObject catalogInfo = catalogsJsonArray.getJSONObject(i);
				CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
				aCatalogPriceBean.setCatalogId(catalogInfo.getString("CatalogId"));
				aCatalogPriceBean.setPrice(catalogInfo.getDouble("Price"));
				catalogPriceBeans.add(aCatalogPriceBean);
			}
			productPriceBean.setCatalogPriceBeans(catalogPriceBeans);

		} else if (dataObj.has("ProductPriceList")) {
			// prodId list
			JSONObject retBean = dataObj.getJSONArray("ProductPriceList").getJSONObject(0);
			String productId = retBean.getString("ProductId");
			productPriceBean.setProductId(productId);

			JSONArray catalogsJsonArray = retBean.getJSONArray("Catalogs");
			productPriceBean.setProductId(retBean.getString("ProductId"));

			List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
			for (int i = 0; i < catalogsJsonArray.length(); i++) {
				JSONObject catalogInfo = catalogsJsonArray.getJSONObject(i);
				CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
				aCatalogPriceBean.setCatalogId(catalogInfo.getString("CatalogId"));
				aCatalogPriceBean.setPrice(catalogInfo.getDouble("Price"));
				catalogPriceBeans.add(aCatalogPriceBean);
			}
			productPriceBean.setCatalogPriceBeans(catalogPriceBeans);

		} else if (dataObj.has("CatalogPriceList")) {
			// catalog list
			JSONArray CatalogPriceListArray = dataObj.getJSONArray("CatalogPriceList");
			String productId = CatalogPriceListArray.getJSONObject(0).getString("ProductId");

			// 组装报文返回值
			productPriceBean.setProductId(productId);

			List<CatalogPriceBean> catalogPriceBeans = new ArrayList<>();
			for (int i = 0; i < CatalogPriceListArray.length(); i++) {
				JSONObject tempCatalogInfo = CatalogPriceListArray.getJSONObject(i);
				JSONObject tempCatalog = tempCatalogInfo.getJSONObject("CatalogInfo");
				CatalogPriceBean aCatalogPriceBean = new CatalogPriceBean();
				aCatalogPriceBean.setCatalogId(tempCatalog.getString("CatalogId"));
				aCatalogPriceBean.setPrice(tempCatalog.getDouble("Price"));
				catalogPriceBeans.add(aCatalogPriceBean);
			}
			productPriceBean.setCatalogPriceBeans(catalogPriceBeans);
		}

		return productPriceBean;
	}

	public static void checkPriceByProd(List<Integer> userIds, List<String> productIds) {
		userIds.forEach(userId -> {
			productIds.forEach(prodId -> {
				System.out.println("BuyId:" + userId + ",  ProductId:" + prodId);

				//compare(new ComparePrice().old_ByProdIds, new ComparePrice().new_ByProdIds, userId,prodId,"GetPriceByProdIds");

				//compare(new ComparePrice().old_ByProdIdsTradeIsolation, new ComparePrice().new_ByProdIdsTradeIsolation, userId, prodId,"GetPriceByProdIdsTradeIsolation");

				try {
					compare(old_ByProdId, new_ByProdId, userId,prodId,"GetPriceByProdId");
					compare(old_ByProdIdTradeIsolation,new_ByProdIdTradeIsolation, userId, prodId,"GetPriceByProdIdTradeIsolation");

				} catch (JSONException e) {
					e.printStackTrace();
				}


				System.out.println("\n");

			});
		});
	}

	public static void checkPriceByCatalog(List<Integer> userIds, List<String> catalogIds) {
		userIds.forEach(userId -> {
			catalogIds.forEach(catalogId -> {
				System.out.println("BuyId:" + userId + ",  CatalogId:" + catalogId);

				try {
					compare(old_ByCatalogIds, new_ByCatalogIds, userId, catalogId, "GetPriceByCatalogIds");
					compare(old_ByCatalogIdsTradeIsolation, new_ByCatalogIdsTradeIsolation, userId, catalogId,
							"GetPriceByCatalogIdsTradeIsolation");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			

				System.out.println("\n");

			});
		});

	}

	public static void main(String[] args) {
		FileUtil.deleteFile(errorFile);
		FileUtil.deleteFile(diffFile);
		FileUtil.deleteFile(retFile);

		String folder = "E:\\fyf\\Testing\\source\\YmatouTest\\Results\\productprice.iapi.ymatou.com\\ComparePrice";
		String userPath = folder + "\\userId.txt";
		String productPath = folder + "\\productId.txt";

		List<Integer> userIdsFromFile = new ArrayList<>();
		FileUtil.readFileAsLine(userPath).forEach(ele -> userIdsFromFile.add(Integer.parseInt(ele)));

		List<String> productsFromFile = FileUtil.readFileAsLine(productPath);
		checkPriceByProd(userIdsFromFile, productsFromFile);

		
	/*	  String catalogPath = folder +"\\catalogId.txt"; List<String>
		  catalogsFromFile = FileUtil.readFileAsLine(catalogPath);

		  checkPriceByCatalog(userIdsFromFile, catalogsFromFile);*/
		 

	}
}
